{extend name="app/admin/view/base.html"/}
{block name="resources"}
<style>
	.recommend-mark{
		background: #FF6A00;
		color: #fff;
		line-height: 1;
		padding: 3px 6px;
		border-radius: 3px;
		font-size: 12px;
		margin-right: 3px;
	}
</style>
{/block}
{block name="main"}

<!-- 搜索框 -->
<!--<div class="ns-single-filter-box">
	<button class="layui-btn ns-bg-color" onclick="addLevel()">添加会员卡</button>

	<div class="layui-form">
		<div class="layui-input-inline">
			<input type="text" name="search_text" placeholder="请输入会员卡名称" autocomplete="off" class="layui-input">
			<button type="button" class="layui-btn layui-btn-primary" lay-filter="search" lay-submit>
				<i class="layui-icon">&#xe615;</i>
			</button>
		</div>
	</div>
</div>-->

<table id="level_list" lay-filter="level_list"></table>

<!-- 操作 -->
<script type="text/html" id="operation">
	<div class="ns-table-btn">

		<a class="layui-btn" lay-event="edit">编辑</a>
<!--		<a class="layui-btn" lay-event="del">删除</a>-->
		<!--{{# if(d.status == 0){ }}
		<a class="layui-btn" lay-event="sale">发售</a>
		{{# } else { }}
		<a class="layui-btn" lay-event="offsale">下架</a>
		{{# } }}-->
	</div>
</script>
{/block}
{block name="script"}
<script>
    layui.use(['form'], function() {
        var table,
            form = layui.form,
            repeat_flag = false; //防重复标识

        table = new Table({
            elem: '#level_list',
            url: ns.url("supermember://admin/membercard/lists"),
            cols: [
                [{
                    field: 'level_name',
                    title: '名称',
                    width: '15%',
                    templet: function(data){
                   		if (data.is_recommend) {
							return '<span class="recommend-mark">推荐</span>' + data.level_name
						} else {
                   		    return data.level_name;
						}
					}
                },{
                    field: 'growth',
                    title: '价格',
                    width: '20%',
                    templet: function(data){
                       /* var charge_rule = JSON.parse(data.charge_rule),
                            price = '';
                        if (charge_rule.week) price += '周卡' + charge_rule.week + '元；';
                        if (charge_rule.month) price += '月卡' + charge_rule.month + '元；';
                        if (charge_rule.quarter) price += '季卡' + charge_rule.quarter + '元；';
                        if (charge_rule.year) price += '年卡' + charge_rule.year + '元；';
                        return price;*/
						return data.level_price;
                    }
                },/*{
                    field: '',
                    title: '权益',
                    width: '30%',
                    templet: function (data) {
                        var text = '';
                        if (data.is_free_shipping == 1) text += '购物享商品包邮；';
                        if (data.consume_discount != 100) text += '购物享'+ (data.consume_discount / 10) +'折优惠；';
                        if (data.point_feedback > 0) text += '购物享'+ data.point_feedback +'倍积分回馈；';
                        return text;
                    }
                },*/
				{
					field: 'member_num',
					title: '持有人数',
					width: '8%',
					unresize: 'false',
					align: 'center'
				},
				{
					title: '状态',
					width: '10%',
					unresize: 'false',
                    templet: function (data) {
                       return data.status == 1 ? '发售中' : '已下架';
                    }
				},{
                    title: '操作',
                    width: '10%',
                    unresize: 'false',
                    toolbar: '#operation'
                }]
            ]
        });


        /**
         * 工具栏操作，编辑、删除
         */
        table.tool(function(obj) {
            var data = obj.data,
                event = obj.event;

            switch (event) {
                case 'edit':
                    location.href = ns.url("supermember://admin/membercard/edit?id=" + data.level_id);
                    break;
                case 'del':
                    delMemberLevel(data.level_id);
                    break;
                case 'sale':
                    saleMemberCard(data.level_id);
                    break;
                case 'offsale':
                    offsaleMemberCard(data.level_id);
                    break;
                case 'recommend':
                    recommendMemberCard(data.level_id, 1);
                    break;
                case 'cancelRecommend':
                    recommendMemberCard(data.level_id, 0);
                    break;
            }
        });

        /**
         * 搜索功能
         */
        form.on('submit(search)', function(data) {
            table.reload( {
                page: {
                    curr: 1
                },
                where: data.field
            });
        });

        // 删除方法
        function delMemberLevel(level_id) {
            if(repeat_flag) return false;
            repeat_flag = true;

            layer.confirm('确定要删除该会员卡吗?', function() {
                $.ajax({
                    type: 'POST',
                    url: ns.url("supermember://admin/membercard/delete"),
                    data: {level_id},
                    dataType: 'JSON',
                    success: function(res) {
                        layer.msg(res.message);
                        repeat_flag = false;

                        if(res.code == 0){
                            table.reload();
                        }

                    }
                });
            }, function () {
                layer.close();
                repeat_flag = false;
            });
        }

        //使失效
        function offsaleMemberCard(level_id) {
            layer.confirm('此操作不会影响到已购卡用户！确定下架该会员卡吗?', function() {
                if (repeat_flag) return;
                repeat_flag = true;

                $.ajax({
                    url: ns.url("supermember://admin/membercard/status"),
                    data: {
                        level_id: level_id
                    },
                    dataType: 'JSON',
                    type: 'POST',
                    success: function(res) {
                        layer.msg(res.message);
                        repeat_flag = false;
                        if (res.code == 0) {
                            table.reload();
                        }
                    }
                });
            }, function() {
                layer.close();
                repeat_flag = false;
            });
        }

        function saleMemberCard(level_id) {
            layer.confirm('确定发售该会员卡吗?', function() {
                if (repeat_flag) return;
                repeat_flag = true;

                $.ajax({
                    url: ns.url("supermember://admin/membercard/status"),
                    data: {
                        status: 1,
                        level_id: level_id
                    },
                    dataType: 'JSON',
                    type: 'POST',
                    success: function(res) {
                        layer.msg(res.message);
                        repeat_flag = false;
                        if (res.code == 0) {
                            table.reload();
                        }
                    }
                });
            }, function() {
                layer.close();
                repeat_flag = false;
            });
        }

        function recommendMemberCard(level_id, recommend) {
            let message = recommend == 1 ? '设置后前端推荐开卡处将默认展示该卡，是否继续设置?' : '是否取消推荐？';
            layer.confirm(message, function() {
                if (repeat_flag) return;
                repeat_flag = true;

                $.ajax({
                    url: ns.url("supermember://admin/membercard/recommend"),
                    data: {
                        level_id: level_id,
                        recommend: recommend
                    },
                    dataType: 'JSON',
                    type: 'POST',
                    success: function(res) {
                        layer.msg('设置成功');
                        repeat_flag = false;
                        if (res.code == 0) {
                            table.reload();
                        }
                    }
                });
            }, function() {
                layer.close();
                repeat_flag = false;
            });
        }

    });

    /**
     * 点击跳转添加会员卡页面
     */
    function addLevel() {
        location.href = ns.url("supermember://admin/membercard/add");
    }

    function showDemo(){
        layer.open({
            title: false,
            type: 1,
            area: ['760px', '570px'],
            content: '<img src="SUPERMEMBER_IMG/demo.png">'
        })
    }
</script>
{/block}